<?php
class Common_model extends CI_Model{
    protected $_gallery_path = "";
	protected $_gallery_url = "";
    function  __construct(){
        parent::__construct();
        $this->_gallery_url = base_url()."upload/avartars/";
		//Lấy đường dẫn vật lý của thư mục chứa hình ảnh đươc upload
		$this->_gallery_path = realpath(APPPATH. "../upload/avartars");
    }
    function getdata($table,$where='',$select='',$order='',$limit='',$or_where=''){
        if(!empty($where)){
            $this->db->where($where);
        }
		if(!empty($or_where)){
			$this->db->or_where($or_where);
		}
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function get_data($table,$where='',$order='',$limit='',$select='',$srart_limit=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order['truong'],$order['sapxep']);
        }
        if(!empty($limit)){
            if(empty($srart_limit)){
                $this->db->limit($limit);
            }else $this->db->limit($limit,$srart_limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }

        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data;
        }else return false;
    }
    function do_upload($width,$height){
		$config = array('upload_path'   => $this->_gallery_path,
						'allowed_types' => 'gif|jpg|png',
						'max_size'      => '2000');
		$this->load->library('upload',$config);
        if(!$this->upload->do_upload('avartar')){
			$error = array($this->upload->display_errors());
		}
        else
        {
			$image_data = $this->upload->data();	
		}
		//kết thúc công đoạn upload hình ảnh
		$config = array("source_image" => $image_data['full_path'],
						"maintain_ration" => true,
						"width" => $width,
						"height" => $height);
		$this->load->library("image_lib",$config);
		$this->image_lib->resize();
		//kết thúc công đoạn resize lại hình ảnh	
        return $image_data;			
	}
    function get_images(){
		$file = scandir($this->_gallery_path);
		$file = array_diff($file, array('.', '..', 'thumbs'));
		//tên tất cả các file hình trong thư mục đã được upload lên.
		
		
		$images = array();
		foreach($file as $img){
			$images[] = array("url"        => $this->_gallery_url . $img,
							  "thumb_url" => $this->_gallery_url . "thumbs/" . $img);	
		}
		return $images;
	}
    function get_data_news($table,$where='',$order='',$limit='',$select='',$srart_limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        if(!empty($order)){
            $this->db->order_by($order['truong'],$order['sapxep']);
        }
        if(!empty($limit)){
            if(empty($srart_limit)){
                $this->db->limit($limit);
            }else $this->db->limit($limit,$srart_limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data;
        }else return false;
    }
    function getdatain($table,$where=array(),$order='',$limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function getdata_in($table,$where=array(),$where_a='',$order='',$limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        if(!empty($where_a)){
            $this->db->where($where_a);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function getone($table,$where='',$select='',$or_where=''){
        if(!empty($where)){
            $this->db->where($where);
        }
		if(!empty($or_where)){
			$this->db->or_where($or_where);
		}
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->row();
            $query->free_result();
            return $data;
        }else return false;
    }
    function getid($table,$where='',$select=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->row();
            $query->free_result();
            return $data->id;
        }else return false;
    }
    function insertdata($table,$data=array()){
        $this->db->insert($table, $data);
    }
    function updatedata($table,$where ='',$data=array()){
        $this->db->where($where);
        $this->db->update($table, $data);
    }
    function deletedata($table,$where){
        $this->db->where($where);
        $this->db->delete($table);
    }
    function getnumber($table,$where = ''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
    function getnumber2($table,$where=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
    function getsum($fiel,$where,$table){
        $this->db->select_sum($fiel);
        $this->db->where($where);
        $query = $this->db->get($table);
        return $query->row()->$fiel;
    }
    function check_trung($nd,$table){
        $this->db->where($nd);
        $query = $this->db->get($table);
        if($query->num_rows()>0){
            $query->free_result();
            return true;
        }return false;
    }
    function gia_km($price, $sale)
    {
        $ketqua =$price - ($price*$sale)/100;
        return $ketqua;
    }
     function percent($num,$sum)
        {
            if($num !=0)
            {
                $percent = ($num/$sum)*100;
                return $percent;
            }
            else
            {
                return 0;
            }
        }
    function rand_string($length) {
        $chuoi = '';
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $size = strlen( $chars );
        for( $i = 0; $i < $length; $i++ ) {
        $chuoi .= $chars[ rand( 0, $size - 1 ) ];
         }
        return $chuoi;
    }
}
function get_thumnails($link,$width='',$height='')
    {
        if(!empty($link))
            {
                $images =base_url().'thumb.php?src='.$link.'&amp;w='.$width.'&amp;h='.$height.'&amp;q=90';
            }
        else
            {
                $noimages = base_url().'skin/front-end/images/no.jpg';
                $images=base_url().'thumb.php?src='.$noimages.'&amp;w='.$width.'&amp;h='.$height.'&amp;q=90';
                
            }
        return $images;
    }
?>